import os
import json
import pandas as pd

# 定义包含所有文件夹路径的父目录
parent_directory = './'

# 存储提取的结果
data = []

# 遍历每个文件夹
for folder in os.listdir(parent_directory):
    folder_path = os.path.join(parent_directory, folder)
    
    # 确保这是一个文件夹
    if os.path.isdir(folder_path):
        stats_file = os.path.join(folder_path, 'stats.json')
        
        # 确保 stats.json 文件存在
        if os.path.exists(stats_file):
            with open(stats_file, 'r') as f:
                stats = json.load(f)
                
                # 提取需要的字段
                row = {
                    'folder_name': folder_path,
                    'experiment_sequence': stats.get('experiment_sequence', None),
                    'start_time': stats.get('start_time', None),
                    'end_time': stats.get('end_time', None),
                    'total_time': stats.get('total_time', None),
                    'timer_frequency': stats.get('timer_frequency', None),
                    'average_sending_rate': stats.get('average_sending_rate', None),
                    'signal_source_mode': stats.get('signal_source_mode', None),
                    'signal_source': stats.get('signal_source', None),
                    'received_count': stats.get('received_count', None),
                    'total_packets': stats.get('total_packets', None),
                    'received_percentage': stats.get('received_percentage', None),
                    'program start time': stats.get('program start time', None),
                    'sending start time': stats.get('sending start time', None),
                    'collecting start time': stats.get('collecting start time', None),
                }
                
                # 将提取的内容添加到数据列表中
                data.append(row)

# 将数据转换为 pandas DataFrame
df = pd.DataFrame(data)

# 输出表格
print(df)

# 如果需要，可以将表格保存为 CSV 文件
df.to_csv('stats_summary.csv', index=False)
